package com.chx.three.mama.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 与时间相关的操作
 */
public class TimeHelper {
    private static Context mContext;
    private  Calendar calendar;
    private static TimeHelper timeHelper;
    private static SharedPreferences sp;

    private TimeHelper(Context context){
        mContext=context;
        sp=context.getSharedPreferences("mama", Context.MODE_PRIVATE);

    }
    public static void initTimeHelper(Context context){
        timeHelper=new TimeHelper(context);
    }
    public static TimeHelper getInstance(){
        if(mContext==null){
            throw new IllegalArgumentException("must init timeHelper before use");
        }
        return timeHelper;
    }

    /**
     * 返回当前毫秒数
     * @return
     */
    public long getCurrentMillisTime(){
        calendar = Calendar.getInstance();
        long timeInMillis = calendar.getTimeInMillis();
        return timeInMillis;
    }

    /**
     * 返回格式化的当前时间
     * @return 返回的时间格式为 2015-03-22
     */
    public String getCurrentData(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(getCurrentMillisTime());
        return format.format(date);
    }

    //====================底下是存储上一次刷新的时间，和获取刷新时间===========
    //...................圈子列表最新帖子.........................
    /**
     * 设置圈子列表最新发帖的时间
     */
    public void setRefreshRingBbsTime(){
        if(sp==null){
            throw new IllegalArgumentException("you must initSPHelper before use it");
        }
        SharedPreferences.Editor editor=sp.edit();
        editor.putLong("refreshRingBbsTime",getCurrentMillisTime());
        editor.commit();
    }

    /**
     * 获取圈子列表最近帖子的上次刷新时间
     * @return
     */
    public String getRefreshRingBbsTime(){
        long refreshRingBbsTime = sp.getLong("refreshRingBbsTime", getCurrentMillisTime());
        long time=getCurrentMillisTime()-refreshRingBbsTime;
        return getMillTimeToString(time);
    }

    //........................圈子列表最近回复...............................
    /**
     * 设置圈子列表最新回复的时间
     */
    public void setRefreshRingReplyTime(){
        if(sp==null){
            throw new IllegalArgumentException("you must initSPHelper before use it");
        }
        SharedPreferences.Editor editor=sp.edit();
        editor.putLong("refreshRingReplyTime",getCurrentMillisTime());
        editor.commit();
    }

    /**
     * 获取圈子列表最近回复的上次刷新时间
     * @return
     */
    public String getRefreshRingReplyTime(){
        long refreshRingReplyTime = sp.getLong("refreshRingReplyTime", getCurrentMillisTime());
        long time=getCurrentMillisTime()-refreshRingReplyTime;
        return getMillTimeToString(time);
    }



    /**
     * 利用毫秒数来获取中文时间，如1天2小时34分
     * @return
     */
    private String getMillTimeToString(long time){
        if(time<60000){
            return "刚刚更新";
        }
        long minute=time/60000;
        if(minute<60){
            return minute+"分钟前更新";
        }
        long hour=minute/60;
        if(hour<24){
            return hour+"小时"+(minute%60)+"分钟前更新";
        }
        long day=hour/24;
        return day+"天"+(hour%24)+"小时前更新";
    }
}


